package com.century.sjt.adapter;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.century.sjt.R;
import com.century.sjt.entity.BankCard;

import java.util.List;

/**
 * Created by luodong on 2016/1/7.
 */
public class BankCardAdapter extends BaseAdapter {

    private LayoutInflater mLayoutInflater;
    // 映射数据
    private List<BankCard> mDataList;
    private Context context;
    private RequestQueue mQueue;
    private String recommendName;


    public BankCardAdapter(Context context, List<BankCard> list, RequestQueue mQueue) {
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        mDataList = list;
        this.mQueue = mQueue;
    }

    // 获取数据量
    @Override
    public int getCount() {
        return mDataList.size();
    }

    // 获取对应ID项对应的Item
    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    // 获取对应项ID
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            //由于我们只需要将XML转化为View，并不涉及到具体的布局，所以第二个参数通常设置为null
            convertView = mLayoutInflater.inflate(R.layout.sjt_bank_card_a_item, null);
            holder.tvBankName = (TextView) convertView.findViewById(R.id.tv_bankname);
            holder.tvCardType = (TextView) convertView.findViewById(R.id.tv_cardtype);
            holder.tvBankCardNo = (TextView) convertView.findViewById(R.id.tv_bankcardno);
            holder.tvBankImage = (ImageView) convertView.findViewById(R.id.a_yh_image);
//            holder.tvQuickPay = (ImageView) convertView.findViewById(R.id.bankcard_quick);
            holder.icon_kjzfsvg = (TextView) convertView.findViewById(R.id.icon_kjzfsvg);
            holder.tv_shortcut= (TextView) convertView.findViewById(R.id.tv_shortcut);
            Typeface iconfont=Typeface.createFromAsset(context.getAssets(),"iconfont.ttf");
            holder.tv_shortcut.setTypeface(iconfont);
            holder.icon_kjzfsvg.setTypeface(iconfont);
            holder.layoutBankCard_bg = (LinearLayout) convertView.findViewById(R.id.layout_bankcard_bg);
            holder.icon_more_1 = (TextView) convertView.findViewById(R.id.tv_bankcard_more_1);
            holder.icon_more_2 = (TextView) convertView.findViewById(R.id.tv_bankcard_more_2);
            holder.icon_more_3 = (TextView) convertView.findViewById(R.id.tv_bankcard_more_3);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.icon_more_1.setTextColor(Color.argb(150, 255, 255, 255));   //文字透明度
        holder.icon_more_2.setTextColor(Color.argb(150, 255, 255, 255));   //文字透明度
        holder.icon_more_3.setTextColor(Color.argb(150, 255, 255, 255));   //文字透明度
//        Log.e("mDataList",mDataList.get(position).getBankName().toString()+"1");
        String status = mDataList.get(position).getStatus();
        holder.tvBankName.setText(mDataList.get(position).getCardName());
        holder.icon_kjzfsvg.setVisibility(View.GONE);
        holder.tv_shortcut.setVisibility(View.GONE);
        switch (status){
            case "0":
                String Type1 = " ("+context.getResources().getString(R.string.text_general_pay)+")";
                holder.tvCardType.setText("("+mDataList.get(position).getCardTypeName()+")");
                break;
            case "1":
                String Type2 = " ("+context.getResources().getString(R.string.text_quicker_pay)+")";
                holder.tvCardType.setText("("+mDataList.get(position).getCardTypeName()+")");
                holder.icon_kjzfsvg.setVisibility(View.VISIBLE);
                holder.tv_shortcut.setVisibility(View.VISIBLE);

                break;
            default:
                holder.tvCardType.setText("("+mDataList.get(position).getCardTypeName()+")");
                break;
        }

        String cardNo=mDataList.get(position).getBankCardNo();
        cardNo=cardNo.substring(cardNo.length()-4,cardNo.length());
        holder.tvBankCardNo.setText(cardNo);
        String[] bankCodes = context.getResources().getStringArray(R.array.bankcode);
        TypedArray ar = context.getResources().obtainTypedArray(R.array.bankicon);
        TypedArray bg = context.getResources().obtainTypedArray(R.array.bankcard_bg);
        for(int i = 0;i<bankCodes.length;i++){
            if(bankCodes[i].toString().equals(mDataList.get(position).getBankCode().toString())){
                holder.tvBankImage.setImageResource(ar.getResourceId(i, 0));
                holder.layoutBankCard_bg.setBackgroundResource(bg.getResourceId(i,0));
                break;
            }else {
                holder.tvBankImage.setImageDrawable(context.getResources().getDrawable(R.drawable.bankicon_yl));
                holder.layoutBankCard_bg.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bankbg_other));
            }
        }




        return convertView;
    }
    // ViewHolder用于缓存控件
    class ViewHolder {
        public TextView tvBankName;
        public TextView tvCardType;
        public TextView tvBankCardNo;
        TextView tv_shortcut;
        public ImageView tvBankImage;
        public LinearLayout layoutBankCard_bg;
        TextView icon_kjzfsvg;
        TextView icon_more_1;
        TextView icon_more_2;
        TextView icon_more_3;
    }

}
